package com.token.sysflow.core;

import com.token.sql.IntegrateSql;
import com.token.sysflow.exception.ErrorCodeEnum;
import com.token.sysflow.exception.FlowException;
import com.token.sysflow.hander.ApproveProcess;
import com.token.sysflow.impl.AbstractFlowHandlerImpl;
import com.token.sysflow.impl.GetPathApproverImpl;

/**
 * 获取提交框的办理人和流向
 *
 * @author chenjianxiong 2020/4/13
 */
public class GetPathApprover extends AbstractFlowCore {
    private final boolean isFirst;

    /**
     * @param f
     * @param keyUnid
     * @throws Exception
     */
    public GetPathApprover(IntegrateSql f, String keyUnid) throws Exception {
        super(f, keyUnid);
        isFirst = false;
    }

    /**
     * @param f
     */
    public GetPathApprover(IntegrateSql f) throws Exception{
        super(f);
        isFirst = true;

    }

    @Override
    public void execute(AbstractFlowHandlerImpl flowHandler) throws Exception {
        GetPathApproverImpl getPathApprover = (GetPathApproverImpl) flowHandler;

        if (isFirst) {
            //初始化起草人、申请人、当前人。
            initUserInfo(getPathApprover.getCreater(),getPathApprover.getApplyer(),flowHandler.getActionUser());

            //流程
            this.setFromFlowById(getPathApprover.getFlowId());

            //环节
            String stepId = getPathApprover.getStepId();
            if(stepId!= null && !"".equals(stepId)){
                this.setFromStepById(getPathApprover.getStepId());
            }else {
                this.setFromStep(this.getFromFlow().getStartStep());
            }

        }else{

            //检查流程状态，结束和终止不允许操作
            checkFlowStatus();

            //判断当前用户是否是当前办理人
            if (!approveProcess.isStepApproves(flowHandler.getActionUser(), ApproveProcess.STATUS_IND)) {
                throw new FlowException(ErrorCodeEnum.CHECK_APPROVE,flowHandler.getActionUser());
            }

            //初始化起草人、申请人、当前人。
            initUserInfo(approveProcess.getCreater(),approveProcess.getApplyer(),flowHandler.getActionUser());


        }

    }
}
